//
//  ST_JVPayItemView.swift
//  btvideo
//
//  Created by mac on 2019/12/11.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import HandyJSON
import SnapKit
import SwifterSwift


class ST_JVPayItemView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
       
        self.backgroundColor = Color.white
        
        self.addSubview(border_view)
        self.addSubview(shadow_view)
        
        border_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        shadow_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(duration_label)
        duration_label.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(40)
        }
     
        self.addSubview(total_price_label)
        total_price_label.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(duration_label.snp.bottom)
            make.height.equalTo(40)
        }
        
        self.addSubview(price_per_month_label)
        price_per_month_label.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.top.equalTo(total_price_label.snp.bottom)
        }
        
        
        self.addSubview(selected_btn)
        selected_btn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - bind Model
    var model = ST_JVPayItemViewModel(){
        didSet{
            
            let limited_time_str = model.limited_time.int.string
            let durationLabelString = NSMutableAttributedString(string: limited_time_str)
            
            let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
            
            durationLabelString.addAttributes(attr, range: NSRange(location: 0, length: limited_time_str.count))
            
            let durationLabelUnitStr = NSMutableAttributedString(string: "个月")
            let attr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1),NSAttributedString.Key.baselineOffset:0]
            durationLabelUnitStr.addAttributes(attr1, range: NSRange(location: 0, length: 2))
            durationLabelString.append(durationLabelUnitStr)
            
            duration_label.attributedText = durationLabelString
            
            
            let perMouthPrefixAttrString = NSMutableAttributedString(string: "￥")
            let perMouthPrefixAttr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 12),.foregroundColor: UIColor.white]
            perMouthPrefixAttrString.addAttributes(perMouthPrefixAttr, range: NSRange(location: 0, length: perMouthPrefixAttrString.length))
            
            let perMouth = model.original_price / (model.limited_time.int == 0 ? 1 : model.limited_time.int).double
            let perMouthAttrString = NSMutableAttributedString(string: perMouth.int.string)
            let perMouthAttr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 28, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
            perMouthAttrString.addAttributes(perMouthAttr, range: NSRange(location: 0, length: perMouthAttrString.length))
            
            let perMouthSuffixAttrString = NSMutableAttributedString(string: "/月")
            perMouthSuffixAttrString.addAttributes(perMouthPrefixAttr, range: NSRange(location: 0, length: perMouthSuffixAttrString.length))
            
            
            perMouthPrefixAttrString.append(perMouthAttrString)
            perMouthPrefixAttrString.append(perMouthSuffixAttrString)
            
            per_mouth_atrribute_white = perMouthPrefixAttrString
            
            
            
            
            let perMouthPrefixAttrString1 = NSMutableAttributedString(string: "￥")
            let perMouthPrefixAttr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 12),.foregroundColor: UIColor.init(hexString: "#999999")!]
            perMouthPrefixAttrString1.addAttributes(perMouthPrefixAttr1, range: NSRange(location: 0, length: perMouthPrefixAttrString1.length))
            
            
            let perMouthAttrString1 = NSMutableAttributedString(string: perMouth.int.string)
            let perMouthAttr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 28, weight: UIFont.Weight.medium),.foregroundColor: UIColor.init(hexString: "#333333")!]
            perMouthAttrString1.addAttributes(perMouthAttr1, range: NSRange(location: 0, length: perMouthAttrString1.length))
            
            let perMouthSuffixAttrString1 = NSMutableAttributedString(string: "/月")
            perMouthSuffixAttrString1.addAttributes(perMouthPrefixAttr1, range: NSRange(location: 0, length: perMouthSuffixAttrString1.length))
            
            
            perMouthPrefixAttrString1.append(perMouthAttrString1)
            perMouthPrefixAttrString1.append(perMouthSuffixAttrString1)
            
            
            per_mouth_atrribute_black = perMouthPrefixAttrString1
            
            if self.selected_btn.isSelected {
                total_price_label.attributedText = per_mouth_atrribute_white
            }else{
                total_price_label.attributedText = per_mouth_atrribute_black
            }
            
            
            
            
            let str = "特惠￥\(model.original_price.int.string)"

            let thString = NSMutableAttributedString(string: str)
      
            let thStringAttr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)]
            
            thString.addAttributes(thStringAttr, range: NSRange(location: 0, length: thString.length))
            
            price_per_month_label.attributedText = thString
        }
    }
//    MARK: - Properties
    
    fileprivate lazy var duration_label:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        return control
    }()
    
    fileprivate let total_price_normal_text_color = UIColor.init(hexString: "#333333")!
    
    fileprivate let total_price_selected_text_color = UIColor.white
    
    
    fileprivate let total_price_normal_background_color = UIColor.clear
    
    fileprivate let total_price_selected_background_color = UIColor.init(hexString: "#FEC207")!

    
    fileprivate var per_mouth_atrribute_white:NSAttributedString?
    
    fileprivate var per_mouth_atrribute_black:NSAttributedString?
    
    fileprivate lazy var total_price_label:UILabel = {
        let control = UILabel.init()
        control.textColor = total_price_normal_text_color
        control.backgroundColor = total_price_normal_background_color
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.textAlignment = .center
        return control
    }()
    
    
    fileprivate lazy var price_per_month_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.init(hexString: "#999999")!
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        control.textAlignment = .center
        return control
    }()
    
    
    lazy var selected_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(click(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var border_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        control.borderColor = Color.init(hexString: "#FEC207")
        control.borderWidth = 2
        control.cornerRadius = 10
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var shadow_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        control.shadowColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 0.4)
        control.shadowOffset = CGSize(width: 0, height: 2)
        control.shadowOpacity = 1
        control.shadowRadius = 10
        return control
    }()
    
//    MARK: - Event
    
    var click_closure:(()->())?
    
    @objc func click(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        
        click_closure?()
        
        modify_status(select: true)
    }
        
    @objc func modify_status(select:Bool){
        
        if select{
           
            self.border_view.isHidden = false
            self.shadow_view.isHidden = true
            
            self.total_price_label.textColor = total_price_selected_text_color
            self.total_price_label.backgroundColor = total_price_selected_background_color
            
        }
        else{
           
            self.border_view.isHidden = true
            self.shadow_view.isHidden = false
            
            
            self.total_price_label.textColor = total_price_normal_text_color
            self.total_price_label.backgroundColor = total_price_normal_background_color
        }
        
        self.selected_btn.isSelected = select
        
        
        
        if self.selected_btn.isSelected {
            total_price_label.attributedText = per_mouth_atrribute_white
        }else{
            total_price_label.attributedText = per_mouth_atrribute_black
        }
        
    }
    

}


//MARK: -
class ST_JVPayItemViewModel: NSObject,HandyJSON {
    
    var id = ""
    var original_price = 0.1
    var limited_time = 0.1
    var ename = ""
    var thrift = ""
    
    var topTitle = ""
    var descr = ""
    
    required override init() {
        super.init()
    }
}
